/***************************************************************************
 *   Copyright (C) 2009 by maarten,,,   *
 *   maarten@maarten-laptop   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "cgpanel/raytracer/ray.h"
#include "objects/vertex.h"
#include <iostream>
using namespace std;

namespace draw {

Ray::Ray()
{
    m_direction = NULL;
    m_origin = NULL;
    m_direction= new object::Vertex();
}

Ray::Ray(object::Vertex *origin, object::Vertex *destination):m_origin(origin)
{
    float xDir = destination->x() - origin->x(); 
    float yDir = destination->y() - origin->y(); 
    float zDir = destination->z() - origin->z(); 

    
    m_direction = new object::Vertex(xDir, yDir, zDir,0);
    m_direction->normalize();
    inv_xDir = (float)1/m_direction->x();
    inv_yDir = (float)1/m_direction->y();
    inv_zDir = (float)1/m_direction->z();
    
    deleteOrigin = true;
}

Ray::Ray(object::Vertex *origin, object::Vertex *direction, bool
dir):m_origin(origin)
{
  m_direction = direction;
  m_direction->setH(0);
  m_direction->normalize();
  inv_xDir = (float)1/m_direction->x();
  inv_yDir = (float)1/m_direction->y();
  inv_zDir = (float)1/m_direction->z();
  
  deleteOrigin = false;
}

void Ray::init(object::Vertex *origin, object::Vertex *destination)
{
  //cout << "hierio1" << endl;
  m_origin = origin;
  float xDir = destination->x() - origin->x(); 
  float yDir = destination->y() - origin->y(); 
  float zDir = destination->z() - origin->z(); 

  m_direction->setValues(xDir, yDir, zDir,0);
  m_direction->normalize();
  inv_xDir = 1.0/m_direction->x();
  inv_yDir = 1.0/m_direction->y();
  inv_zDir = 1.0/m_direction->z(); 
}

Ray::~Ray()
{
  if(m_direction != NULL)
    delete m_direction;
  if(m_origin != NULL && deleteOrigin)
    delete m_origin;
}


}
